<?php

namespace app\api\service;

use app\api\model\ExaminationPaper;
use app\api\model\ExaminationQuestion;
use app\api\model\UserExamination;

class ExaminationService
{
    public static function getHotExamination($params)
    {
        $hotList = UserExamination::getHotList($params['page_no'], $params['page_size']);
        if (empty($hotList)) {
            $list = ExaminationPaper::getListByWhere([['a.status', '=', 1], ['a.cid', '>', 1]], $params['page_no'], $params['page_size']);
            $hotList = array_map(function ($item) {
                return [
                    'exam_id' => $item['id'],
                    'exam_title' => $item['title'],
                    'cate_title' => $item['category_title'],
                    'exam_thumb' => dealImage($item['thumb']),
                    'join_number' => 0
                ];
            }, $list);
        }
        return [200, '操作成功', $hotList];
    }

    public static function getExamList($params)
    {
        $where = [];
        if (!empty($params['keyword'])) $where[] = ['a.title', 'like', '%' . $params['keyword'] . '%'];
        $where[] = ['a.status', '=', 1];
        $where[] = ['a.cid', '>', 1];
        $list = ExaminationPaper::getListByWhere($where, $params['page_no'], $params['page_size']);
        $data = array_map(function ($item) {
            return [
                'exam_id' => $item['id'],
                'exam_title' => $item['title'],
                'cate_title' => $item['category_title'],
                'exam_thumb' => dealImage($item['thumb']),
                'join_number' => UserExamination::countExaminationUser($item['id']),
            ];
        }, $list);
        return [200, '操作成功', $data];
    }

    public static function getExamInfo($params)
    {
        $examInfo = ExaminationPaper::getExaminationInfoById($params['examination_id']);
        if (empty($examInfo)) return [201, '考卷不存在', []];
        $data = ExaminationQuestion::getCountAndTotalByPid($params['examination_id']);
        $formConfig = [
            'labelPosition' => 'top',
            'size' => '',
            'labelAlign' => 'label-left-align',
            'layoutType' => 'Pad',
            'exercId' => $examInfo['id'],
            'exercTitle' => $examInfo['title'],
            'exercCount' => $data['numbers'] ?? 0,
            'exercScore' => $data['scores'] ?? 0,
            'exercTime' => $examInfo['answer_time'],
        ];
        $questionList = ExaminationQuestion::getQuestionListByPid($params['examination_id']);
        shuffle($questionList);
        return [200, '操作成功', ['widgetList' => $questionList, 'formConfig' => $formConfig]];
    }

    public static function subExamInfo($params, $userInfo)
    {
        $examInfo = ExaminationPaper::getExaminationInfoById($params['examination_id']);
        if (empty($examInfo)) return [201, '考卷不存在', []];
        $res = UserExamination::insert([
            'uid' => $userInfo['id'],
            'pid' => $params['examination_id'],
            'score' => $params['score'],
            'create_time' => time(),
        ]);
        if ($res) return [200, '操作成功', []];
        return [201, '提交失败', []];
    }
}